package com.framework.modules.base.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.Query;
import com.framework.modules.base.dao.UserInfoDao;
import com.framework.modules.base.entity.UserInfoEntity;
import com.framework.modules.base.service.UserBalanceService;
import com.framework.modules.base.service.UserInfoService;
import com.framework.modules.base.service.UserService;
import com.framework.modules.dataview.service.ViewUserInfoSpaceService;
import com.framework.modules.sys.service.SysRegionAreaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;


@Service("userInfoService")
public class UserInfoServiceImpl extends ServiceImpl<UserInfoDao, UserInfoEntity> implements UserInfoService{

	@Autowired
	private UserService userService;

	@Autowired
	private UserBalanceService userBalanceService;

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private SysRegionAreaService sysRegionAreaService;


	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Page<UserInfoEntity> page = this.selectPage(
				new Query<UserInfoEntity>(params).getPage(),
				new EntityWrapper<UserInfoEntity>()
		);

		return new PageUtils(page);
	}

	@Override
	@Transactional
	public Object saveRegisterInfo(Long userid,UserInfoEntity entity) {
		if(entity==null){
			return "参数为空";
		}
		if(userid!=null){
			entity.setUid(userid.intValue());
			//2.查询用户名重名
			if(StringUtils.isNotBlank(entity.getNickname())){
				if(baseMapper.queryByNickname(entity.getNickname())!=null){
					return "用户名重复";
				}
			}
			entity.setLevel(1);
			entity.setUid(userid.intValue());
			return baseMapper.insert(entity);
		}
		return "用户信息不存在";
	}

	@Override
	public Object getInfo(Long userid,String type) {
		if (userid == null) {
			return "token有误";
		}
		Object result;
		switch (type) {
			case "full":
				Map<String, Object> paraMap = new HashMap<>();
				paraMap.put("base", userService.selectById(userid));
				paraMap.put("info", userInfoService.selectById(userid));
				paraMap.put("balance", userBalanceService.selectById(userid));
				result = paraMap;
				break;
			case "base":
				result = userService.selectById(userid);
				break;
			case "info":
				result = userInfoService.selectById(userid);
				break;
			case "balance":
				result = userBalanceService.selectById(userid);
				break;
			default:
				result = "参数有误";
		}
		return result;
	}

	@Override
	public Object saveUserInfo(Long uid, UserInfoEntity entity) {
		UserInfoEntity userInfoEntity=baseMapper.selectById(uid);

		String nikeName=entity.getNickname();
		if(StringUtils.isNotBlank(nikeName)){
			if(baseMapper.queryByNickname(nikeName)!=null){
				return "用户名重复";
			}
			userInfoEntity.setNickname(nikeName);
		}

		if(null==userInfoEntity){
			return "用户不存在";
		}

		if(StringUtils.isNotBlank(entity.getAvanta())){
			userInfoEntity.setAvanta(entity.getAvanta());
		}
		if(StringUtils.isNotBlank(entity.getSchool())){
			userInfoEntity.setSchool(entity.getSchool());
		}
		if(StringUtils.isNotBlank(entity.getBirthday())){
			userInfoEntity.setBirthday(entity.getBirthday());
		}
		String area=entity.getArea();
		if(StringUtils.isNotBlank(area)&&StringUtils.isNumeric(area)){
			userInfoEntity.setArea(sysRegionAreaService.getRegionAreaDetail(Integer.parseInt(area)));
		}
		if(StringUtils.isNotBlank(entity.getIbo())){
			userInfoEntity.setIbo(entity.getIbo());
		}
		if(entity.getGender()!=null){
			userInfoEntity.setGender(entity.getGender());
		}
		return baseMapper.updateById(userInfoEntity);
	}

	@Override
	public Integer updateUserInfo(UserInfoEntity userInfoEntity) {
		return this.baseMapper.updateById(userInfoEntity);
	}

	@Override
	public Object updateUserNickname(Long uid, String nickname) {
		if(StringUtils.isBlank(nickname)){
			return "昵称不能为空";
		}
		UserInfoEntity userInfoEntity=this.baseMapper.queryByNickname(nickname);
		if(userInfoEntity!=null){
			return "昵称不可用";
		}
		UserInfoEntity entity=new UserInfoEntity();
		entity.setNickname(nickname);
		entity.setUid(uid.intValue());
		return baseMapper.updateById(entity);
	}

	@Override
	public Object getOtherUserInfo(Long uid) {
		return baseMapper.getOtherUserInfo(uid);
	}

	@Override
	public Object getRecommandUserList(Integer limit,Long uid) {
		return baseMapper.getRecommandUserList(limit,uid);
	}
}
